package cn.lwj.service.impl;

import cn.lwj.common.R;
import cn.lwj.dto.DishDto;
import cn.lwj.mapper.CategoryMapper;
import cn.lwj.mapper.DishMapper;
import cn.lwj.pojo.Category;
import cn.lwj.pojo.Dish;
import cn.lwj.pojo.DishFlavor;
import cn.lwj.service.CategoryService;
import cn.lwj.service.DishFlavorService;
import cn.lwj.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;


//    由于涉及多张表 保证数据的一致性 所以需要启动事务管理
    @Transactional
    @Override
    public void saveDishWithDishFlavor(DishDto dishDto) {
//        1.新增菜品
        this.save(dishDto);
//        2.新增菜品口味
//        新增口味之前需要提供菜品的id  新增菜品成功后dishDto对象已经存储了id 所以可直接使用
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     */
    @Override
    public void page(int page, int pageSize, String name) {

    }

    @Transactional
    @Override
    /**
     * 修改菜品 涉及多张表随意开启事务管理
     */
    public void updateDish(DishDto dishDto) {
        Dish dish = new Dish();
//        BeanUtils.copyProperties(dishDto,dish);
        BeanUtils.copyProperties(dishDto,dish,"flavors","categoryName","copies");
//        更新菜品信息
        this.updateById(dish);
//        删除菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
//        设置新的口味
//        这里相当于新增口味口味还没有设置菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(dishDto.getId());
//        }
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(String ids) {

        int i = ids.indexOf(",");
//        System.out.println("i == " + i);
        if(i == -1){
//            单个删除
            //        1.根据菜品id删除菜品对应的口味
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,ids);
            dishFlavorService.remove(lqw);
            //        2.删除菜品
            this.removeById(ids);
            return;
        }

//        批量删除

//        1.删除菜品对应的口味
        String[] idsArr = ids.split(",");
        for (String dishId : idsArr) {
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,dishId);
            dishFlavorService.remove(lqw);
        }

//        2.删除多个菜品
        ArrayList<String> list = new ArrayList<>();
        for (String id : idsArr) {
            list.add(id);
        }
        this.removeByIds(list);
    }


    @Transactional
    @Override
    public void updateStatus(int statusCode, String ids) {
        //        log.info(statusCode);
        /*
            code = 1 起售
            code = 0 停售
         */
        int i = ids.indexOf(","); // 如果等于-1说明是单个停售或起售
        if(i == -1){
//            单个状态修改
            //                1.根据id查询菜品
            Dish dish = this.getById(ids);
            dish.setStatus(statusCode);
            this.updateById(dish);
            return;
        }
//        批量状态修改
        String[] idsArr = ids.split(",");
//            批量起售停售
        for (String id : idsArr) {
            Dish dish = this.getById(id);
            dish.setStatus(statusCode);
            this.updateById(dish);
        }
    }


}
